using UnityEngine;

public class BallState_Shoot : AbstractBallState
{
	public static BallState_Shoot instance;

	private Vector3 posTarget;

	private float timeTotal;

	private float timeInAir;

	private BallState_Shoot()
	{
	}

	public static BallState_Shoot getInstance()
	{
		if (instance == null)
		{
			instance = new BallState_Shoot();
		}
		return instance;
	}

	public override void enter(Ball owner)
	{
		base.enter(owner);
		owner.isInSomeOneHand = true;
		owner.player_make_ball_outside = owner.shoot_player;
		owner.isMeetScoreCondition = false;
		owner.isReachUpSideOfBasketry = false;
		owner.isReachMiddleSideOfBasketry = false;
	}

	public override void exit(Ball owner)
	{
		base.exit(owner);
		owner.isShooting = false;
	}

	public override void fixedUpdate(Ball owner)
	{
		base.fixedUpdate(owner);
		if (owner.isShooting)
		{
			if (owner.isMeetScoreCondition && owner.isWillScore)
			{
				owner.OnBallIn();
				return;
			}
			if (timeInAir >= timeTotal)
			{
				Vector3 position = owner.getPosition();
				if (position.y < 2.9f)
				{
					owner.isShooting = false;
					if (owner.isMeetScoreCondition && owner.isWillScore)
					{
						owner.OnBallIn();
					}
					else
					{
						owner.OnBallOut();
					}
					return;
				}
			}
			timeInAir += Time.fixedDeltaTime;
			owner.useGravity();
		}
		else
		{
			if (owner.isInSomeOneHand)
			{
				return;
			}
			if (owner.isDunk)
			{
				owner.OnBallIn();
				owner.GetComponent<Rigidbody>().velocity = Vector3.down * 3f;
				if (owner.shoot_player.teamType == TeamType.USER)
				{
					Instance<Court>.instance.count_PauseData_User[3]++;
				}
				else
				{
					Instance<Court>.instance.count_PauseData_Com[3]++;
				}
				Instance<AudioController>.instance.play(AudioType.Sound_dunk);
			}
			else
			{
				calValues(owner);
			}
		}
	}

	private void calValues(Ball owner)
	{
		owner.isWillScore = DSS.IsWillScore(owner.shoot_player, owner.isLayUp, owner.isFreeThrow);
		if (owner.isWillScore)
		{
			posTarget = ((!owner.isShootLeft) ? MathHelper.setOffestY(Global.posBallIn_Right_Basketry, -0.1f) : MathHelper.setOffestY(Global.posBallIn_Left_Basketry, -0.1f));
			if (owner.isLayUp && MathHelper.isInPercent(30f))
			{
				posTarget = ((!owner.isShootLeft) ? MathHelper.setOffestY(Global.posBallIn_Right_Backboard, -0.1f) : MathHelper.setOffestY(Global.posBallIn_Left_Backboard, -0.1f));
			}
		}
		else
		{
			posTarget = ((!owner.isShootLeft) ? Global.posBallOut_Right[Random.Range(0, Global.posBallOut_Left.Length)] : Global.posBallOut_Left[Random.Range(0, Global.posBallOut_Left.Length)]);
		}
		owner.isShooting = true;
		addForce(owner, posTarget);
		Instance<Court>.instance.labelShootPerecnt.gameObject.SetActiveRecursively(state: true);
		Instance<Court>.instance.labelShootPerecnt.text = owner.shootPercent + "%";
		Transform transform = Instance<Court>.instance.labelShootPerecnt.transform;
		Vector3 vector = Instance<CameraController>.instance.cameraMain.WorldToScreenPoint(owner.getPosition());
		float x = vector.x / (float)Screen.width * 800f - 400f;
		Vector3 vector2 = Instance<CameraController>.instance.cameraMain.WorldToScreenPoint(owner.getPosition());
		transform.localPosition = new Vector3(x, vector2.y / (float)Screen.height * 480f - 240f, 0f);
		TweenPosition component = Instance<Court>.instance.labelShootPerecnt.GetComponent<TweenPosition>();
		component.enabled = true;
		component.from = Instance<Court>.instance.labelShootPerecnt.transform.localPosition;
		component.to = MathHelper.setOffestY(Instance<Court>.instance.labelShootPerecnt.transform.localPosition, 15f);
		component.duration = 1f;
		component.ResetToBeginning();
		component.PlayForward();
		TweenAlpha component2 = Instance<Court>.instance.labelShootPerecnt.GetComponent<TweenAlpha>();
		component2.delay = 0.7f;
		component2.enabled = true;
		component2.from = 1f;
		component2.to = 0f;
		component2.duration = 0.5f;
		component2.ResetToBeginning();
		component2.PlayForward();
		Instance<AudioController>.instance.play(AudioType.Sound_layup);
	}

	private void addForce(Ball owner, Vector3 posTarget)
	{
		float num = Vector3.Distance(MathHelper.setY(posTarget, 0f), MathHelper.setY(owner.getPosition(), 0f));
		float y = posTarget.y;
		Vector3 position = owner.getPosition();
		float num2 = y - position.y;
		float num3 = 2f * num2 / num + 1f;
		Vector3 a = MathHelper.setOffestY(posTarget, num * num3 - num2);
		float num4 = 9.8f;
		float num5 = num * num * num4 / (2f * (num * num3 - num2));
		float num6 = num5 * num3 * num3;
		float num7 = Mathf.Sqrt(num5 + num6);
		owner.setVelocityZero();
		if (!float.IsNaN(a.x))
		{
			Vector3 position2 = owner.getPosition();
			if (!float.IsNaN(position2.x) && !float.IsNaN(num7))
			{
				owner.GetComponent<Rigidbody>().AddForce((a - owner.getPosition()).normalized * num7, ForceMode.VelocityChange);
			}
		}
		if (!float.IsNaN(num5))
		{
			timeTotal = num / Mathf.Sqrt(num5);
		}
		else
		{
			timeTotal = 1f;
		}
		if (float.IsNaN(timeTotal))
		{
			timeTotal = 1f;
		}
		timeInAir = 0f;
	}
}
